package com.crm.manager.service.impl;

import com.crm.common.core.page.TableDataInfo;
import com.crm.manager.domain.data.DataStatistics;
import com.crm.manager.domain.dto.ClueStatisticPageDto;
import com.crm.manager.domain.dto.ContractStatisticPageDto;
import com.crm.manager.domain.vo.ClueStatisticPageVO;
import com.crm.manager.domain.vo.ContractStatisticPageVo;
import com.crm.manager.domain.vo.StatisticsVo;
import com.crm.manager.mapper.ClueMapper;
import com.crm.manager.mapper.ContractMapper;
import com.crm.manager.service.ClueService;
import com.crm.manager.service.ReportService;
import com.crm.manager.utils.TimeIntervalString;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static com.github.pagehelper.page.PageMethod.startPage;

@Service
@Slf4j
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {


    private final ContractMapper contractMapper;
    private final ClueMapper clueMapper;

    // 客户统计
    @Override
    public StatisticsVo contractStatistics(LocalDate beginTime, LocalDate endTime) {
        StatisticsVo statisticsVo = new StatisticsVo();

        // 调用工具类，获得时间间隔的字符串数组
        String[] timeIntervals = TimeIntervalString.getTimeIntervalsAsStringArray(beginTime,endTime);
        statisticsVo.setXAxis(timeIntervals);

        List<DataStatistics> clientDataList = new ArrayList<>();

        DataStatistics clientData1 = new DataStatistics();
        // 获得每天新增的客户数
        clientData1.setName("新增客户数");
        List<Integer> clientCounts = new ArrayList<>() ;
        for (String timeInterval : timeIntervals) {
          clientCounts.add(contractMapper.getEveryDayClientCounts(timeInterval));
        }

        clientData1.setData(clientCounts.toArray(new Integer[0]));
        clientDataList.add(clientData1);

        DataStatistics clientData2 = new DataStatistics();
        // 获得客户总数
        clientData2.setName("客户总数");
            // 获得当前的客户总数
        Integer currentCount = contractMapper.getClientTotals(timeIntervals[0]);
        List<Integer> clientTotals = new ArrayList<>();
        clientTotals.add(currentCount);
        for (int i = 1; i < clientCounts.size(); i++) {
            currentCount = currentCount + clientCounts.get(i);
            clientTotals.add(currentCount);
        }
        clientData2.setData(clientTotals.toArray(new Integer[0]));
        clientDataList.add(clientData2);

        DataStatistics[] series = clientDataList.toArray(new DataStatistics[0]);
        statisticsVo.setSeries(series);
        return statisticsVo;
    }

    // 线索统计
    @Override
    public StatisticsVo cluesStatistics(LocalDate beginTime, LocalDate endTime) {

        StatisticsVo statisticsVo = new StatisticsVo();
        // 调用工具类，获得时间间隔的字符串数组
        String[] timeIntervals = TimeIntervalString.getTimeIntervalsAsStringArray(beginTime,endTime);
        statisticsVo.setXAxis(timeIntervals);
        List<DataStatistics> clueDataList = new ArrayList<>();

        // 新增线索数量
        DataStatistics newClueData = new DataStatistics();
        newClueData.setName("新增线索数量");
        List<Integer> newClueCount = new ArrayList<>();
        for (String timeInterval : timeIntervals) {
            newClueCount.add(clueMapper.getEveryDayClueCount(timeInterval));
        }
        newClueData.setData(newClueCount.toArray(new Integer[0]));
        clueDataList.add(newClueData);

        // 线索总数量
        DataStatistics clueCountData = new DataStatistics();
        clueCountData.setName("线索总数量");
            // 获得当前线索的总数量
        Integer currentClueCount = clueMapper.getCurrentClueCount(timeIntervals[0]);
        List<Integer> clueCountTotal = new ArrayList<>();
        clueCountTotal.add(currentClueCount);
        for (int i = 1; i < newClueCount.size(); i++) {
            currentClueCount = currentClueCount + newClueCount.get(i);
            clueCountTotal.add(currentClueCount);
        }
        clueCountData.setData(clueCountTotal.toArray(new Integer[0]));
        clueDataList.add(clueCountData);

        statisticsVo.setSeries(clueDataList.toArray(new DataStatistics[0]));
        return statisticsVo;
    }

    // 客户统计分页查询
    @Override
    public TableDataInfo clientStatisticPageQuery(ContractStatisticPageDto dto) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<ContractStatisticPageVo> list = contractMapper.clientStatisticPageQuery(dto);
        Page<ContractStatisticPageVo> page = (Page<ContractStatisticPageVo>) list;
        tableDataInfo.setCode(200);
        tableDataInfo.setRows(page.getResult());
        tableDataInfo.setTotal(page.getTotal());
        return tableDataInfo;
    }

    // 线索统计分页查询
    @Override
    public TableDataInfo clueStatisticPageQuery(ClueStatisticPageDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        List<ClueStatisticPageVO> list = clueMapper.clueStatisticPageQuery(dto);
        Page<ClueStatisticPageVO> page = (Page<ClueStatisticPageVO>) list;
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getResult());
        return tableDataInfo;
    }
}
